package com.tiku.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tiku.common.ResultJson;
import com.tiku.controller.CO.SetmealInfoCO;
import com.tiku.controller.TO.SetmealTO;
import com.tiku.entity.ProductSpecifications;
import com.tiku.entity.SingleSubjectQuestionBanks;
import com.tiku.entity.TikuImg;
import com.tiku.entity.TypeClassChecked;
import com.tiku.mapper.ProductSpecificationsMapper;
import com.tiku.mapper.TikuImgMapper;
import com.tiku.mapper.TypeClassMapper;
import com.tiku.service.SingleSubjectQuestionBanksService;
import com.tiku.mapper.SingleSubjectQuestionBanksMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.stream.Stream;

/**
 *
 */
@Service
public class SingleSubjectQuestionBanksServiceImpl extends ServiceImpl<SingleSubjectQuestionBanksMapper, SingleSubjectQuestionBanks>
        implements SingleSubjectQuestionBanksService {


    @Autowired
    private SingleSubjectQuestionBanksMapper mapper;

    @Autowired
    private TypeClassMapper typeClassMapper;

    @Autowired
    private TikuImgMapper tikuImgMapper;

    @Autowired
    private ProductSpecificationsMapper specificationsMapper;

    @Override
    public ResultJson getExaminationList(String examId) {
        List<SingleSubjectQuestionBanks> singleSubjectQuestionBanks = mapper.selectList(new LambdaQueryWrapper<>(SingleSubjectQuestionBanks.class).eq(SingleSubjectQuestionBanks::getProfessionCategoriesId, examId));
        return ResultJson.success(singleSubjectQuestionBanks);


    }

    @Override
    public ResultJson setmealList(String setmealTO) {
        List<SingleSubjectQuestionBanks> singleSubjectQuestionBanks = mapper.selectList(new LambdaQueryWrapper<>(SingleSubjectQuestionBanks.class)
                .eq(SingleSubjectQuestionBanks::getProfessionCategoriesId, setmealTO));

        for (SingleSubjectQuestionBanks banks : singleSubjectQuestionBanks) {
            String tagList = banks.getTagList();
            List<String> strings = JSON.parseArray(tagList).toJavaList(String.class);
            banks.setTagLists(strings);
        }
        return ResultJson.success(singleSubjectQuestionBanks);
    }

    @Override
    public ResultJson setmealInfo(SetmealTO setmealTO) {
        String setmealId = setmealTO.getSetmealId();
        //获取题库标题
        SingleSubjectQuestionBanks singleSubject = mapper.selectById(setmealId);
        if (Objects.isNull(singleSubject)){
            return ResultJson.error("服务器开小差了，请稍后再试试");
        }
        String tagList = singleSubject.getTagList();
        List<String> strings = JSON.parseArray(tagList).toJavaList(String.class);
        singleSubject.setTagLists(strings);
        singleSubject.setTagList(null);

        //获取所有分类
        List<TypeClassChecked> typeClassChecked = typeClassMapper.getCheckedInfo(setmealId);

        String setmealTypeId = setmealTO.getSetmealTypeId();




        if (StringUtils.isEmpty(setmealTypeId)) {
            for (TypeClassChecked classChecked : typeClassChecked) {
                if (classChecked.getChecked()==1) {
                    setmealTypeId=classChecked.getTypeId();
                }
            }
        } else {
            for (TypeClassChecked classChecked : typeClassChecked) {
                classChecked.setChecked(0);
                if (classChecked.getTypeId().equals(setmealTypeId)) {
                    classChecked.setChecked(1);
                }
            }
        }
        //获取题库图片
        TikuImg tikuImg = tikuImgMapper.selectById(setmealId);

        List<ProductSpecifications> productSpecifications = specificationsMapper.getSetmealInfoList(setmealTypeId);
        SetmealInfoCO co = combineData(singleSubject, typeClassChecked, tikuImg, productSpecifications);


        return ResultJson.success(co);
    }

    /**
     * 拼接info数据
     *
     * @param singleSubject
     * @param typeClassChecked
     * @param tikuImg
     * @param productSpecifications
     * @return
     */
    private SetmealInfoCO combineData(SingleSubjectQuestionBanks singleSubject, List<TypeClassChecked> typeClassChecked, TikuImg tikuImg, List<ProductSpecifications> productSpecifications) {
        SetmealInfoCO co = new SetmealInfoCO();
        //拼接外层
        co.setName(singleSubject.getName());
        co.setTagList(singleSubject.getTagLists());
        co.setPrice(productSpecifications.get(0).getDisPrice());
        co.setSellingPrice(productSpecifications.get(0).getSellingPrice());
        co.setEffectiveDays(365);
        co.setContent(tikuImg.getContent());
        co.setMobileShowImg(tikuImg.getMobileShowImg());


        //设置typeList
        co.setTypeList(typeClassChecked);

        //设置specList
        co.setSpecList(productSpecifications);


        return co;

    }


}




